package com.jiayang.hhotel.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.view.menu.MenuAdapter;
import android.support.v7.widget.GridLayoutManager;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.DividerItemDecoration;
import com.jiayang.hhotel.adapter.SetPermissionsAdapter;
import com.jiayang.hhotel.bean.BuildingStyle;
import com.jiayang.hhotel.listener.OnItemClickListener;
import com.jiayang.hhotel.presenter.PSetPermissions;
import com.yanzhenjie.recyclerview.swipe.Closeable;
import com.yanzhenjie.recyclerview.swipe.OnSwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/10 0010.
 *
 */

public class SetPermissionsActivity extends XActivity<PSetPermissions>{
    private Activity mContext;
    private SetPermissionsAdapter mMenuAdapter;
    SwipeMenuRecyclerView menuRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private List<BuildingStyle> mDataList =new ArrayList<>();
    BuildingStyle buildingStyle1,buildingStyle2,buildingStyle3,buildingStyle4,buildingStyle5,buildingStyle6;
    @Override
    public void initData(Bundle savedInstanceState) {

        initDatas();
        mContext=this;

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.setp_swipe_layout);
        mSwipeRefreshLayout.setOnRefreshListener(mOnRefreshListener);
        menuRecyclerView = (SwipeMenuRecyclerView) findViewById(R.id.setp_recycler_view);

        menuRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));// 布局管理器。

        //menuRecyclerView.addItemDecoration(new DividerItemDecoration(this,6,getResources().getColor(R.color.titlebackgrond)));// 添加分割线。

        // 为SwipeRecyclerView的Item创建菜单就两句话，不错就是这么简单：
        // 设置菜单创建器。
       menuRecyclerView.setSwipeMenuCreator(swipeMenuCreator);
        // 设置菜单Item点击监听。
       menuRecyclerView.setSwipeMenuItemClickListener(menuItemClickListener);

        mMenuAdapter = new SetPermissionsAdapter(mDataList,mContext);
        mMenuAdapter.setOnItemClickListener(onItemClickListener);
        menuRecyclerView.setAdapter(mMenuAdapter);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_setpermissions;
    }

    @Override
    public PSetPermissions newP() {
        return null;
    }



    /**
     * 刷新监听。
     */
    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            menuRecyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }, 2000);
        }
    };


    /**
     * Item点击监听。
     */
    private OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            Toast.makeText(getApplicationContext(), "我目前是第" + position + "条。", Toast.LENGTH_SHORT).show();
        }
    };

    /**
     * 菜单点击监听。
     */
    private OnSwipeMenuItemClickListener menuItemClickListener = new OnSwipeMenuItemClickListener() {
        @Override
        public void onItemClick(Closeable closeable, int adapterPosition, int menuPosition, int direction) {
            closeable.smoothCloseMenu();// 关闭被点击的菜单。

            if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                Toast.makeText(mContext, "list第" + adapterPosition + "; 右侧菜单第" + menuPosition, Toast.LENGTH_SHORT).show();
            } else if (direction == SwipeMenuRecyclerView.LEFT_DIRECTION) {
                Toast.makeText(mContext, "list第" + adapterPosition + "; 左侧菜单第" + menuPosition, Toast.LENGTH_SHORT).show();
            }
        }
    };

    /**
     * 菜单创建器。
     */
    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.item_height);
            // MATCH_PARENT 自适应高度，保持和内容一样高；也可以指定菜单具体高度，也可以用WRAP_CONTENT。
            int height = ViewGroup.LayoutParams.MATCH_PARENT;

            // 添加左侧的，如果不添加，则左侧不会出现菜单。

            {
                    SwipeMenuItem addItem = new SwipeMenuItem(mContext)
                            .setBackgroundDrawable(R.drawable.selector_green)// 点击的背景。
                            .setImage(R.mipmap.ic_action_add) // 图标。
                            .setWidth(width) // 宽度。
                            .setHeight(height); // 高度。
                    swipeLeftMenu.addMenuItem(addItem); // 添加一个按钮到左侧菜单。


                SwipeMenuItem closeItem = new SwipeMenuItem(mContext)
                        .setBackgroundDrawable(R.drawable.selector_red)
                        .setImage(R.mipmap.ic_action_close)
                        .setWidth(width)
                        .setHeight(height);

                swipeLeftMenu.addMenuItem(closeItem); // 添加一个按钮到左侧菜单。
            }

            // 添加右侧的，如果不添加，则右侧不会出现菜单。

            {
                SwipeMenuItem deleteItem = new SwipeMenuItem(mContext)
                        .setBackgroundDrawable(R.drawable.selector_red)
                        .setImage(R.mipmap.ic_action_delete)
                        .setText("删除") // 文字，还可以设置文字颜色，大小等。。
                        .setTextColor(Color.WHITE)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。

                SwipeMenuItem closeItem = new SwipeMenuItem(mContext)
                        .setBackgroundDrawable(R.drawable.selector_purple)
                        .setImage(R.mipmap.ic_action_close)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(closeItem); // 添加一个按钮到右侧菜单。
            }
       }

    };

    public void initDatas(){

        buildingStyle1=new BuildingStyle(1,"A","中国","China");
        buildingStyle2=new BuildingStyle(1,"A","中国","China");
        buildingStyle3=new BuildingStyle(1,"B","中国","China");
        buildingStyle4=new BuildingStyle(1,"C","中国","China");
        buildingStyle5=new BuildingStyle(1,"D","中国","China");
        buildingStyle6=new BuildingStyle(1,"E","中国","China");

        mDataList.add(buildingStyle6);
        mDataList.add(buildingStyle2);
        mDataList.add(buildingStyle3);
        mDataList.add(buildingStyle1);
        mDataList.add(buildingStyle2);
        mDataList.add(buildingStyle3);
        mDataList.add(buildingStyle4);
        mDataList.add(buildingStyle5);
    }

}
